package com.wyx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyx.Common.Basecontext;
import com.wyx.Common.R;
import com.wyx.Pojo.Employee;
import com.wyx.Service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /*员工登录*/
    @RequestMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request, HttpServletResponse response){
       String password= employee.getPassword();
       password= DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername()).eq(Employee::getPassword,password);
        Employee one = employeeService.getOne(wrapper);
        if (one!=null){
            if (one.getStatus()==1){
                if (request.getSession().getAttribute("employee")!=null){
                    request.getSession().removeAttribute("employee");
                }
                //设置30分钟有效期
//                redisTemplate.opsForValue().set(one.getId(),one.getUsername(),30, TimeUnit.MINUTES);
                request.getSession().setAttribute("employee",one.getId());
                return R.success(one);
            }else {
                return R.error("该用户已被禁用");
            }
        }else {
            return R.error("用户名或密码输入错误");
        }
    }
    /*员工退出*/
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    /*新增员工*/
    @PostMapping
    public R<String> addnewEmployee(HttpServletRequest request,@RequestBody Employee employee){
        String idNumber = employee.getIdNumber();
        String password=idNumber.substring(idNumber.length()-6,idNumber.length());
        employee.setPassword(DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        boolean save = employeeService.save(employee);
        return R.success("新增员工成功");
    }
    /*查询全部 和分页查询 和条件查询*/
    @GetMapping("/page")
    public R<Page> getall(int page,int pageSize,String name){
        Page<Employee> pageinfo=new Page<>(page,pageSize);
        LambdaQueryWrapper <Employee> wrapper=new LambdaQueryWrapper<>();
        if (name==null){
            employeeService.page(pageinfo);
        }else {
            wrapper.like(Employee::getUsername,name);
            employeeService.page(pageinfo,wrapper);
        }
        return R.success(pageinfo);
    }

    /**
     * 修改用户
     * @param employee
     * @return
     */
    @PutMapping
    public R<String>update(@RequestBody Employee employee){
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id获得用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee>getonebyid(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }
}
